package com.babble.user.controller;

import com.babble.user.entity.User;
import com.babble.user.service.IUserService;
import com.babble.user.vo.UserUpdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.springblade.common.exception.MsgException;
import org.springblade.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cld
 */
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("userController")
@Api(value = "用户模块(Controller)", tags = "用户接口")
public class UserController {
	IUserService userService;

	@ApiOperation(value = "注册用户", notes = "账号username:username,密码password:password,注册来源source:source,绑定邮箱email:email,昵称nickName:nickName")
	@PostMapping("/registered")
	public R registered(@Valid @RequestBody User user) throws MsgException {
		userService.registered(user);
		return R.success("注册成功");
	}
	@ApiOperation(value = "修改用户邮箱", notes = "邮箱email:email")
	@PostMapping("/user-updemail")
	public R update(@Valid @Email(message = "格式必须为邮箱") @RequestParam("email") String email) throws MsgException {
		userService.update(email);
		return R.success("邮箱修改完成");
	}

	/**
	 * 判断此次登录的用户是否绑定账户名密码
	 */
	@ApiOperation(value = "查看用户是否绑定了账号")
	@PostMapping("/isBind")
	public R isBind() throws MsgException {
		userService.isBind();
		return R.success("已经绑定");
	}

	@ApiOperation(value = "gitee绑定账号")
	@PostMapping("/bind")
	public R bind(@Valid @Length(min = 9, max = 14, message = "用户名长度9-14") @RequestParam("userName") String userName, @Valid @Length(min = 9, max = 14, message = "密码长度9-14") @RequestParam("password") String password) throws MsgException {
		userService.bind(userName, password);
		return R.success("绑定成功");
	}


	@ApiOperation(value = "修改个人信息", notes = "昵称nickName:nickName,手机mobile:mobile,qqqq:qq,微信wechat:wechat,性别gender:gender,生日birthday:birthday,邮箱email:email,职业occupation:occupation,简介summary:summary")
	@PostMapping("/updteInfo")
	public R updteInfo(@Valid @RequestBody UserUpdVO user) throws MsgException {
		userService.updteInfo(user);
		return R.success("修改成功");
	}

	@ApiOperation(value = "修改个人密码")
	@PostMapping("/updatePwd")
	public R updatePwd(@Valid @Length(min = 9, max = 14, message = "密码长度9-14") @RequestParam("oldPassword") String oldPassword, @Valid @Length(min = 9, max = 14, message = "密码长度9-14") @RequestParam("newPassword") String newPassword) throws MsgException {
		userService.updatePwd(oldPassword, newPassword);
		return R.success("修改成功");
	}
}
